<?php
namespace app\manage\controller;
class ChannelNotice extends Base
{
    //列表
    public function index(){
        if( request()->isAjax() ){
            $page = input('page');
            $limit = input('limit');
            $noticeType = input('notice_type');
            $title = input('title');
            $id = input('id');

            $where['status'] = ['<>', -1];
            !empty($noticeType) && $where['notice_type'] = $noticeType;
            !empty($id) && $where['id'] = $id;
            !empty($title) && $where['title'] = ['like', '%'.$title.'%'];

            if( ! $count=model('ChannelNotice')->where($where)->count() ){
                $this->layTable();
            }

            $select = model('ChannelNotice')
                ->where($where)
                ->order('release_time desc')
                ->limit(($page-1)*$limit, $limit)
                ->select();

            $data = [];
            $noticeType = config('notice_type');
            $noticeGrade = config('notice_grade');

            foreach ($select as $k=>$v){
                $data[] = [
                    'id' => $v['id'],
                    'title' => $v['title'],
                    'author' => $v['author'],
                    'notice_type' => $noticeType[ $v['notice_type'] ],
                    'notice_grade' => $noticeGrade[ $v['notice_grade'] ],
                    'release_time' => $v['release_time'],
                    'status' => $v['status'],
                ];
            }
            $this->layTable(0, '渠道公告列表', $data, $count);
        }else{
            return view();
        }
    }

    //添加
    public function add(){
        if( request()->isAjax() ){
            $addRs = model('ChannelNotice')->allowField(true)->save( input() );
            $addRs ? $this->layJson(0, '添加成功') : $this->layJson(1, model('Channel')->getError());
        }else{
            return view();
        }
    }

    //编辑
    public function edit(){
        if( request()->isAjax() ){
            $editRs = model('ChannelNotice')->allowField(true)->isUpdate(true)->save( input() );
            $editRs!==false ? $this->layJson(0, '编辑成功') : $this->layJson(1, model('Channel')->getError());
        }else{
            $find = model('ChannelNotice')->where('id', input('id'))->find();
            return view('', ['find'=>$find]);
        }
    }

    //删除
    public function del(){
        $delRs = model('ChannelNotice')->allowField(true)->isUpdate(true)->save(['status'=>-1], ['id'=>input('id')]);
        $delRs!==false ? $this->layJson(0, '删除成功') : $this->layJson(1, '删除失败，请重试');
    }
}